<template>
  <div class="wrapper" ref="content">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'
  export default {
    name: "Scroll",
    props: {
      probeType:{
        type: Number,
        default: 0
      },
      pullUpLoad:{
        type: Boolean,
        default: false
      }
    },
    data() {
      //这里存放数据
      return {
        scroll:null
      };
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      //1.创建BScroll对象
      this.scroll = new BScroll(this.$refs.content,{
        click:true,
        probeType: this.probeType,
        pullUpLoad: this.pullUpLoad
      });
      //2.监听滚定区域
      if (this.probeType === 2 || this.probeType ===3) {
        this.scroll.on('scroll',(postion) =>{
          this.$emit('iscroll', postion);
        });
      }
     
      //3.监听scroll滚动到底部
      if (this.pullUpLoad) {
        this.scroll.on('pullingUp',() =>{
          this.$emit('pullingUp');
        })
      }
    },

    methods: {
      //页面下滑移动的距离
      scrollTo(x, y, time=300){
        this.scroll && this.scroll.scrollTo(x,y,time);
      },
      //返回顶部
      finishPullUp(){
        this.scroll.finishPullUp();
      },
      //刷新
      refresh(){
        this.scroll && this.scroll.refresh();
      },
      //获取下滑的距离
      getScrollY(){
        return this.scroll ? this.scroll.y : 0;
      }
    }
  }
</script>
<style scoped>

</style>